package net.bplaced.streetfighter.adapter;

import java.util.List;

import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.lib.model.GoalGetter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.de>
 */
public class GoalGetterAdapter
        extends BaseAdapter {

    private final Context context;
    private final List<GoalGetter> goalgetters;

    public GoalGetterAdapter(final Context context, final List<GoalGetter> goalgetters) {

        this.context = context;
        this.goalgetters = goalgetters;
    }

    public void add(GoalGetter goalGetter) {

        this.goalgetters.add(goalGetter);
        this.notifyDataSetChanged();
    }

    public void addAll(List<GoalGetter> goalgetters) {

        this.goalgetters.addAll(goalgetters);
        this.notifyDataSetChanged();
    }

    public void addAll(GoalGetter[] goalgetters) {

        for (final GoalGetter g : goalgetters) {
            this.goalgetters.add(g);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return this.goalgetters.size();
    }

    @Override
    public GoalGetter getItem(int position) {

        return this.goalgetters.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final GoalGetter g = this.goalgetters.get(position);

        if (convertView == null) {
            final LayoutInflater inflater =
                    (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.goalgetter_adapter_item, null);
        }

        // Name field
        final TextView nameField = (TextView) convertView.findViewById(R.id.goalgetter_adapter_item_name);
        nameField.setText(g.playerName);

        // Goals field
        final TextView goalsField = (TextView) convertView.findViewById(R.id.goalgetter_adapter_item_goals);
        goalsField.setText(g.playerGoals.toString());

        return convertView;
    }
}